﻿using System;
using System.Collections.Generic;
using Core;
using Core.Utils;
using Game.UI.Weapon;
using Unity.VisualScripting;
using UnityEditor.PackageManager.UI;
using UnityEngine;

namespace Game.UI {
    public class UIMgr : Util.Singleton<UIMgr> {
        public Transform Root {
            get;
            private set;
        }

        private Dictionary<Type, string> mWindowNameDic = new Dictionary<Type, string>(){
            {typeof (WeaponEditWindow),"WeaponEditwindow"},
            {typeof (TipsWindow), "TipsWindow"},
            {typeof (ShopWindow),"ShopWindow"}
        };

        private Dictionary<Type, UIWindow> mWindowCacheDic = new Dictionary<Type, UIWindow>();
        protected override void OnCreate() {
            Root = GameObject.Find("UIRoot")?.transform;
            if (!Root) {
                Root = AssetLoader.InstantiateGameObject("UIRoot").transform;
                Root.name = "UIRoot";
            }
        }

        public void OpenWindow<T>(params object[] args) where T : UIWindow{
            GameInput.Ins.CombatInput.Disable();
            if (mWindowCacheDic.TryGetValue(typeof(T),out UIWindow window)) {
                window.Open(args);
                return;
            }
            
            AssetLoader.InstantiateGameObject(mWindowNameDic[typeof(T)], (obj) => {
                window = obj.GetComponent<T>();
                mWindowCacheDic.Add(typeof(T),window);
                window.Open(args);
                window.transform.localPosition = Vector3.zero;
            },Root);
        }

        public void CloseWindow<T>() {
            if (mWindowCacheDic.TryGetValue(typeof(T),out UIWindow window)) {
                window.Close();
            }

            if (!IsAnyOpen()) {
                GameInput.Ins.CombatInput.Enable();
            }
        }

        public void CloseWindow<T>(T obj) {
            if (mWindowCacheDic.TryGetValue(typeof(T),out UIWindow window)) {
                window.Close();
            }

            if (!IsAnyOpen()) {
                GameInput.Ins.CombatInput.Enable();
            }
        }
        
        public bool IsOpen<T>() {
            if (mWindowCacheDic.TryGetValue(typeof(T),out UIWindow window)) {
                return window.IsOpen;
            }
            return false;
        }

        public bool IsOpen(Type type) {
            if (mWindowCacheDic.TryGetValue(type,out UIWindow window)) {
                return window.IsOpen;
            }
            return false;
        }
        
        public bool IsAnyOpen() {
            foreach (var window in mWindowNameDic) {
                if (IsOpen(window.Key)) {
                    return true;
                }
            }

            return false;
        }
        
        public GameObject CreateWorldUIElement(string resName,Vector3 pos = default,Transform parent = null) {
            GameObject obj = PoolMgr.Ins.Spawn(resName);
            obj.transform.SetParent(parent ?? Root);
            pos = pos == default ? Vector3.zero : pos;
            RectTransform transform = obj.GetComponent<RectTransform>();
            Vector2 screenPosition = RectTransformUtility.WorldToScreenPoint(Camera.main, pos);
            RectTransformUtility.ScreenPointToLocalPointInRectangle(Root.GetComponent<RectTransform>(), screenPosition, null, out Vector2 localPosition);
            transform.anchoredPosition = localPosition;
            return obj;
        }
    }
}